package com.ssh.admin.action;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.common.util.JsonResult;
import com.ssh.common.util.Page;
import com.ssh.entity.Seller;
import com.ssh.entity.SellerAssess;
import com.ssh.entity.Task;
import com.ssh.manager.SellerAssessManager;
import com.ssh.manager.SellerManager;
import com.ssh.manager.TaskManager;
import com.ssh.manager.impl.TaskManagerImpl;

@Controller
@ParentPackage("admin")
@Scope("prototype")
@Results(value={
@Result(name="success", location="/admin/seller/sellerAssess.jsp"),
@Result(name="findList", type="json", params={"root","page","excludeproperties","page\\.curPageNum,page\\.totalPageNum,page\\.totalCount," +
"page\\.numberPerPage,page\\.pageData\\[\\d+\\]\\.id,page\\.pageData\\[\\d+\\]\\.yibiao,page\\.pageData\\[\\d+\\]\\.kaoqin,page\\.pageData\\[\\d+\\]\\.createDate," +
"page\\.pageData\\[\\d+\\]\\.zhishi,page\\.pageData\\[\\d+\\]\\.jiqiao,page\\.pageData\\[\\d+\\]\\.seller\\.name,page\\.pageData\\[\\d+\\]\\.seller\\.jobNumber," +
"page\\.pageData\\[\\d+\\]\\.seller\\.post,page\\.pageData\\[\\d+\\]\\.seller\\.id,page\\.pageData\\[\\d+\\]\\.task\\.content,page\\.pageData\\[\\d+\\]\\.task\\.id"}),
@Result(name="update", type="json", params={"root","jr","excludeproperties","jr\\.code"}),
@Result(name="delete", type="json", params={"root","page","excludeproperties","page\\.curPageNum"})
})
public class SellerAssessAction extends ActionSupport{
	@Resource(name="sellerAssessManagerImpl")
	private SellerAssessManager sellerAssessManager;
	@Resource(name="sellerManagerImpl")
	private SellerManager sellerManager;
	@Resource(name="taskManagerImpl")
	private TaskManager taskManager;
	
	private Page page = new Page();
	private JsonResult jr=new JsonResult();
	private String isDesc;
	private Seller seller;
	private Task task;
	private SellerAssess sellerAssess;
	private String ids;
	
	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public JsonResult getJr() {
		return jr;
	}

	public void setJr(JsonResult jr) {
		this.jr = jr;
	}

	public String getIsDesc() {
		return isDesc;
	}

	public void setIsDesc(String isDesc) {
		this.isDesc = isDesc;
	}

	public Seller getSeller() {
		return seller;
	}

	public void setSeller(Seller seller) {
		this.seller = seller;
	}

	public Task getTask() {
		return task;
	}

	public void setTask(Task task) {
		this.task = task;
	}

	public SellerAssess getSellerAssess() {
		return sellerAssess;
	}

	public void setSellerAssess(SellerAssess sellerAssess) {
		this.sellerAssess = sellerAssess;
	}

	public String goJsp(){
		return "success";
	}
	
	public String add(){
		seller = sellerManager.getSellerById(seller.getId());
		task = taskManager.fingTaskById(task.getId());
		sellerAssess.setSeller(seller);
		sellerAssess.setTask(task);
		sellerAssess.setCreateDate(new Timestamp(System.currentTimeMillis()));
		sellerAssessManager.add(sellerAssess);
		return "success";
	}
	
	public String findList(){
		Map conditionMap = new HashMap();
		conditionMap.put("task", task.getContent());
		conditionMap.put("seller", seller.getName());
		sellerAssessManager.findList(conditionMap, page, isDesc);
		return "findList";
	}
	
	public String update(){
		SellerAssess sa = sellerAssessManager.getById(sellerAssess.getId());
		sa.setSeller(sellerManager.getSellerById(seller.getId()));
		sa.setTask(taskManager.fingTaskById(task.getId()));
		sa.setYibiao(sellerAssess.getYibiao());
		sa.setKaoqin(sellerAssess.getKaoqin());
		sa.setZhishi(sellerAssess.getZhishi());
		sa.setJiqiao(sellerAssess.getJiqiao());
		sellerAssessManager.saveUpdate(sa);
		return "update";
	}
	
	public String delete(){
		try {
			if(ids!=null && !ids.equals("")){
				String[] d = ids.split(",");
				for (int i = 0; i < d.length; i++) {
					sellerAssessManager.delete(new Long(d[i]));
				}
			}else{
				sellerAssessManager.delete(sellerAssess.getId());
			}
			
			int _p = (int) page.getCurPageNum();
			page.setCurPageNum(1);
			Map conditionMap = new HashMap();
			conditionMap.put("task", task.getContent());
			conditionMap.put("seller", seller.getName());
			sellerAssessManager.findList(conditionMap, page, isDesc);
			if(_p<=page.getTotalPageNum()){
				page.setCurPageNum(_p);
			}else{
				page.setCurPageNum(_p-1);
			}
			jr.setCode("y");
		} catch (Exception e) {
			jr.setCode("n");
		}
		return "delete";
	}
}
